package cc.rengu.igas.channel.cncp.biz.impl;

import cc.rengu.igas.channel.cpcn.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.cpcn.facade.CpcnTransReconcService;
import cc.rengu.igas.channel.cpcn.facade.request.CpcnPaginationTransReconcRequest;
import cc.rengu.igas.channel.cpcn.facade.response.CpcnPaginationTransReconcResponse;
import cc.rengu.igas.channel.cpcn.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * @author wukefan
 * @version 1.0.0
 * @date 2020/7/24 10:14
 */
public class CpcnTransReconcServiceImpl implements CpcnTransReconcService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<CpcnPaginationTransReconcResponse> cpcnPaginationTransReconc(CpcnPaginationTransReconcRequest cpcnPaginationTransReconcRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CpcnPaginationTransReconcResponse> result = new Result<>();
        CpcnPaginationTransReconcResponse cpcnPaginationTransReconcResponse = new CpcnPaginationTransReconcResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(cpcnPaginationTransReconcRequest,
                    "CpcnPaginationTransReconService", CpcnPaginationTransReconcResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CpcnPaginationTransReconcResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("中金支付-交易对账(分页)失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                cpcnPaginationTransReconcResponse.setRespDesc(((BizException) e).getCode());
                cpcnPaginationTransReconcResponse.setRespDesc(e.getMessage());
            } else {
                cpcnPaginationTransReconcResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                cpcnPaginationTransReconcResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
